﻿using System;
using INdTFramework.Http;
using Newtonsoft.Json;
using RestSharp;

namespace INdTFramework.Http
{
    /// <summary>
    /// HttpRestRequest class specialized for json request and response.
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="E"></typeparam>
    public class DefaultJsonHttpRequest<T, E> : HttpRestRequest<T, E>
    {
        public DefaultJsonHttpRequest(String resource, RestSharp.Method method, Action<T> successResponse, Action<E> errorResponse)
            : base(resource, method, successResponse, errorResponse) {}

        protected override void ProcessSuccessRequest(IRestResponse response, RestRequestAsyncHandle handle)
        {
            _successResponse(JsonConvert.DeserializeObject<T>(response.Content));
        }

        protected override void ProcessFailedRequest(IRestResponse response, RestRequestAsyncHandle handle)
        {
            _errorResponse(JsonConvert.DeserializeObject<E>(response.Content));
        }
    }
}
